package com.sc.outpatientsys.service.impl;

import com.sc.outpatientsys.entity.Doctor;
import com.sc.outpatientsys.entity.Student;
import com.sc.outpatientsys.mapper.DoctorMapper;
import com.sc.outpatientsys.service.IDoctorService;
import com.sc.outpatientsys.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.UnknownServiceException;
import java.util.List;
@Service
public class DoctorServiceImpl implements IDoctorService {
    @Autowired
    private DoctorMapper doctorMapper;
    @Override
    public void reg(Doctor doctor) {
        String username = doctor.getUserName();
        Doctor res = doctorMapper.findByDoctorName(username);
        if(res!=null){
            throw  new UserNameHasExistException("用户名已经存在");
        }
        Integer rows = doctorMapper.insertDoctor(doctor);
        if(rows != 1){
            throw new InsertException("用户注册过程中出现未知错误");
        }
    }

    @Override
    public Doctor login(String username, String password) {
        Doctor doctor = doctorMapper.findByDoctorName(username);
        if(doctor!=null && password.equals(doctor.getPassword()))
            return doctor;
        else {
            throw new PasswordNotMacthException("用户名密码错误");
        }
    }

    @Override
    public void update(Doctor doctor) {
        Integer res = doctorMapper.updateDoctor(doctor);
        if(res!=1){
            throw new UpdateException("修改时发生错误");
        }
    }

    @Override
    public void delete(int dc_ID) {
        Integer rows = doctorMapper.deleteDoctor(dc_ID);
        if(rows!=1){
            throw new DeleteException("删除时出现未知错误");
        }
    }

    @Override
    public Doctor findById(int dc_ID) {
        Doctor result = doctorMapper.findByDoctorID(dc_ID);
        if(result == null){
            throw new NotFindException("未找到");
        }
        return result;
    }

    @Override
    public Doctor findByName(String username) {
        Doctor result = doctorMapper.findByDoctorName(username);
        if(result == null){
            throw new NotFindException("未找到");
        }
        return result;
    }

    @Override
    public List<Doctor> findAll() {
        List<Doctor> result = doctorMapper.selectAllDoctor();
        if(result == null){
            throw new NotFindException("未找到");
        }
        return result;
    }

    @Override
    public void updateAccess(int dc_ID, int access) {
        Integer rows = doctorMapper.updateAccess(dc_ID,access);
        if (rows != 1){
            throw new UpdateException("更新时出现异常");
        }
    }

    @Override
    public List<Doctor> findByClinic(String clinic) {
        List<Doctor> result = doctorMapper.findByClinic(clinic);
        if(result == null){
            throw new NotFindException("未找到相关科室");
        }
        return result;
    }
}
